<template>
  <div>
    <el-dialog
      title="应用素材"
      :visible.sync="dialogVisible2"
      :close-on-click-modal="false"
      :before-close="beforeClose"
      width="70%"
    >
      <div class="filter-container">
        <div class="filter-container">
          <el-button type="primary" @click="dialogVisible1 = true">添加素材</el-button>
        </div>
        <el-table
          :data="materialData"
          row-key="id"
          style="width: 100%"
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="strMaterialname" label="标题" align="center" />
          <el-table-column prop="typeName" label="类型" align="center" />
          <el-table-column prop="strOrgname" label="所属机构" align="center" />
          <el-table-column prop="strDownload" label="下载地址" align="center">
            <template slot-scope="{row}">
              <a :href="row.strDownload" target="_blank" class="link-type">预览</a>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="removeTableColumn(scope.$index)">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer">
        <el-button @click="dialogVisible2 = false,$parent.$data.visible = false">取 消</el-button>
        <el-button type="primary" @click="handleBindAPP">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="新增素材"
      :visible.sync="dialogVisible1"
      width="70%"
      :close-on-click-modal="false"
    >
      <div class="container">
        <el-form :model="addForm" label-width="80px">
          <el-col :span="8">
            <el-form-item label="所属机构" label-width="80px">
              <el-cascader
                v-model="addForm.iOrgsysnum"
                :show-all-levels="false"
                :options="optionsNum"
                :props="{ expandTrigger: 'hover', checkStrictly: true, value:'strOrgnum',label:'strOrgname',children: 'yhlist' }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="strMaterialdetail" label="素材标签">
              <el-select v-model="addForm.strMaterialdetail" placeholder="请选择素材标签">
                <el-option
                  v-for="item in optionTag"
                  :key="item.id"
                  :label="item.strValue"
                  :value="item.strCode"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2" class="searbtn">
            <el-form-item>
              <el-button type="primary" @click="addsearch">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-form>
        <el-table :data="addTableData" style="width: 100%" border @selection-change="changeFun">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="strMaterialname" label="标题" align="center" />
          <el-table-column prop="typeName" label="类型" align="center" />
          <el-table-column prop="strMaterialdetail" label="标签" align="center" />
          <el-table-column prop="strDownload" label="下载地址" align="center">
            <template slot-scope="{row}">
              <a :href="row.strDownload" target="_blank" class="link-type">预览</a>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="addsearchture">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import commonApi from '@/mixins/commonApi'
import { addsearchPrograms } from '@/api/addTag'
import { getOrglist, getSysType } from '@/api/common'
import { addMaterApp } from '@/api/codeapi'
export default {
  // mixins: [commonApi],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    iAppnumTomat: {
      // 应用编号
      type: Number,
      default: () => ''
    },
    fromParentData: {
      //
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      addForm: {
        iOrgsysnum: '',
        strMaterialdetail: '',
        iFlag: 1,
        iAppnum: this.iAppnumTomat
      },
      dialogVisible1: false,
      dialogVisible2: false,
      optionsNum: [],
      optionTag: [],
      addTableData: [],
      materialData: [],
      addListForm: {
        materialList: []
      },
      bindData: {
        iAppnum: '',
        iMaterialnum: ''
      },
      materialSelection: [],
      multipleSelection: []
    }
  },
  created() {
    getOrglist().then(res => {
      this.optionsNum = res.data
    })
    getSysType({ typeInfo: [3] }).then(res => {
      this.optionTag = res.data[3]
    })
    this.dialogVisible2 = this.visible
    if (this.fromParentData.length > 0) {
      this.fromParentData.map(item => {
        this.materialData.push({
          strMaterialname: item.STR_MATERIALNAME,
          typeName: item.APPTYPENAME,
          strOrgname: item.I_ORGSYSNUM,
          strDownload: item.STR_DOWNLOAD,
          iMaterialnum: item.I_MATERIALNUM
        })
      })
    }
  },
  methods: {
    changeFun(val) {
      this.multipleSelection = val
    },
    removeTableColumn(index) {
      this.$confirm('确认移除吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(_ => {
        this.materialData.splice(index, 1)
      })
    },
    addsearch(param) {
      const submitData = JSON.parse(JSON.stringify(this.addForm))
      if (submitData.iOrgsysnum.length > 0) {
        submitData.iOrgsysnum = submitData.iOrgsysnum.splice(-1).join(',')
        addsearchPrograms(submitData).then(res => {
          this.addTableData = res.result
        })
      }
    },

    unique(arr, u_key) {
      const result = []
      result[0] = arr[0]
      arr.forEach((meta_item, i) => {
        // 声明计数变量，如果源数组中的一个对象和result结果数组中的所有对象不同，就push
        let num = 0
        result.forEach((r_item, j) => {
          if (meta_item[u_key] !== r_item[u_key]) {
            num++
          }
          if (num === result.length) {
            result.push(meta_item)
          }
        })
      })
      return result
    },

    // 点击新增素材弹窗确认按钮
    addsearchture() {
      if (this.multipleSelection.length > 0) {
        // this.materialData = this.materialData.concat(this.multipleSelection);
        // this.dialogVisible1 = false
        // this.materialData = this.materialData.concat(this.multipleSelection)
        this.materialData = [...this.materialData, ...this.multipleSelection]

        const ret = this.unique(this.materialData, 'strMaterialname')
        this.materialData = ret
        // const newobj = {}
        // this.materialData.reduce((prev, next)=>{

        // })
        this.addTableData.forEach((item, index) => {
          this.addTableData[index].iPlaytime = ''
          this.addTableData[index].iOrder = ''
        })
        this.dialogVisible1 = false
      } else {
        this.$message({
          type: 'warning',
          message: '请先选择一条素材！'
        })
      }
    },
    handleSelectionChange(val) {
      this.addListForm.materialList = val
    },

    handleBindAPP() {
      if (this.addListForm.materialList.length > 0) {
        const queryArr = []
        this.addListForm.materialList.forEach((item, index) => {
          queryArr.push({
            iAppnum: this.iAppnumTomat,
            iMaterialnum: item.iMaterialnum
          })
        })
        addMaterApp({ refAppList: JSON.stringify(queryArr) }).then(res => {
          const row = { iAppnum: this.iAppnumTomat }
          this.$parent.ShowDetail(row)
          this.$nextTick(_ => {
            this.$parent.$data.visible = false
          })
          // console.log(this.iAppnumTomat)
        })
      } else {
        this.$message({
          type: 'warning',
          message: '请先选择一条素材！'
        })
      }
    },
    beforeClose() {
      this.$parent.$data.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
